package pers.mine.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pers.mine.dao.OrderMapper;
import pers.mine.dao.QueryMapper;
import pers.mine.dto.QueryHandler;
import pers.mine.po.GoodsGroup;
import pers.mine.po.Order;
import pers.mine.po.OrderExample;
import pers.mine.service.inter.OrderService;
import pers.mine.vo.PageInfo;

/**
 * @author Mine
 * @since 2016年10月20日_下午9:30:06
 */
@Service
public class OrderServiceImpl implements OrderService {
	@Autowired
	private OrderMapper om;
	@Autowired
	private QueryMapper qm;
	// 日志记录器
	private static final Log logger = LogFactory
			.getLog(OrderServiceImpl.class);

	/*
	 * @see pers.mine.service.inter.OrderService#addOrder(pers.mine.po.Order)
	 */
	@Override
	public boolean addOrder(Order one) {
		if (one == null) {
			return false;
		}
		// 设置id 库存 为null
		one.setId(null);
		// 设置提交订单时间
		one.setBuyTime(new Date());
		// 使用动态插入语句
		if (om.insertSelective(one) == 1) {
			return true;
		}
		return false;
	}

	/*
	 * @see pers.mine.service.inter.OrderService#getOneOrderByID(int)
	 */
	@Override
	public Order getOneOrderByID(int oid) {
		// 简单验证合法性
		if (oid <= 0) {
			return null;
		}
		return om.selectByPrimaryKey(oid);
	}

	/*
	 * @see pers.mine.service.inter.OrderService#findPageByExample(pers.mine.po.
	 * OrderExample)
	 */
	@Override
	public PageInfo<Order> findPageByExample(OrderExample example) {
		return null;
		// TODO 自动生成的方法存根
		// return om.selectByExample(example);
	}
	
	@Override
	public List<Order> findPageByMid(Integer mid) {
		return om.selectByMid(mid);
	}

	@Override
	public pers.mine.dto.PageInfo<Order> findByQueryHandler(QueryHandler qh) {
		List<Order> list = qm.queryOrder(qh);
		long rootSize = 0;
		int nowPage = 1;
		int pageSize = 5;
		if (qh == null || qh.getLimitSentence() == null) {// 没分页
			if (logger.isDebugEnabled()) {
				logger.debug("构造PageInfo:未指定分页信息...");
			}
			if (list != null) {
				rootSize = list.size();
			}
		} else {
			nowPage = qh.getPageNumber();
			pageSize = qh.getPageSize();
			// 获取总记录数
			rootSize = qm.findLastRows();
		}
		if (logger.isDebugEnabled()) {
			logger.debug("构造PageInfo:rootSize=" + rootSize + ",nowPage="
					+ nowPage + ",pageSize=" + pageSize);
		}
		pers.mine.dto.PageInfo<Order> pi = new pers.mine.dto.PageInfo<Order>((int) rootSize,
				nowPage, pageSize);
		pi.setBasePageList(list);
		return pi;
	}

}
